package com.shane.music
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;

	public class MakeSounds extends Sprite
	{
		import globalVars;
		import com.TopLevel.TopLevel;
		import EventWithParams.EventWithParams;
		public static var music:Sound
		public static var sc:SoundChannel;
		public static var soundEffect:Sound;
		public static var effectSC:SoundChannel;
		public static var request:URLRequest;
		public static var effectRequest:URLRequest;
		public static var soundLength:int = 0;
		public static var ratio:Number;
		public static var savePoint:int;
		
		private var virgin:Boolean = true;
		public static var sTransform:SoundTransform = new SoundTransform(0.5);
		
		public function MakeSounds()
		{
			//TopLevel.stage.addEventListener("hasBeenScrubbed", hasBeenScrubbed, false, 0, true);
			//music.addEventListener(Event.ID3, newID3, false, 0, true);
			addEventListener("newVolume", newVolume);
			
		}
		public function newVolume(e:Event) {
			sc.soundTransform = sTransform;
			effectSC.soundTransform = sTransform;
		}
		public function newSoundEffect($path:String) :void {
			effectRequest = new URLRequest($path);
			soundEffect = new Sound(effectRequest);
			effectSC = soundEffect.play();
			effectSC.soundTransform = sTransform;
		}
		public function playNew($path:String, $startpoint:Number = 0) :void {
			trace("make sound from "+$path);
			if(globalVars.isPlaying) {
				sc.stop();
			}
			if (!this.hasEventListener(Event.ENTER_FRAME))
				this.addEventListener(Event.ENTER_FRAME, broadcastPosition);
			//if (!music.hasEventListener(Event.ID3))
				//music.addEventListener(Event.ID3, newID3, false, 0, true);
			request = new URLRequest($path);
			
			music = new Sound(request);
			sc = music.play($startpoint);
			sc.soundTransform = sTransform;
			
			globalVars.isPlaying = true;
			if(!sc.hasEventListener(Event.SOUND_COMPLETE))
				sc.addEventListener(Event.SOUND_COMPLETE, soundComplete, false, 0, false);
				
			if (virgin) {
				virgin = false;
				TopLevel.stage.addEventListener("pauseSong", pauseSong, false, 0, true);
				TopLevel.stage.addEventListener("playSong", playSong, false, 0, true);	
			}
			
		}
		public function pauseSong(e:Event = null) :void {
			savePoint = sc.position;
			sc.stop();
			globalVars.isPlaying = false;
		}
		
		public function playSong(e:Event = null) :void {
			trace("playSong");
		//	music = new Sound(request);
			sc = new SoundChannel();
			sc.soundTransform = sTransform;
			sc = music.play(savePoint);
			globalVars.isPlaying = true;
			trace("playSong");
		}
		private function newID3 (e:Event) :void {
			soundLength = music.length;
			trace("soundLength found :"+soundLength)
		}
		private function soundComplete (e:Event):void {
			TopLevel.stage.dispatchEvent( new EventWithParams("nextSong", false, false, "auto"));
			//globalVars.isPlaying = false;
			//sc.stop();
		}
		private function broadcastPosition (e:Event) :void {
			soundLength = music.length;
			ratio = sc.position / soundLength;
			
			//trace(ratio)
			//this.dispatchEvent( new EventWithParams("ratioComplete", false , false, ratio));
		}
		public function percentDone () :int {
			ratio = sc.position / soundLength;
			ratio *= 100;
			return(Math.round(ratio));
			//trace(ratio)
			//this.dispatchEvent( new EventWithParams("ratioComplete", false , false, ratio));
		}
		public function hasBeenScrubbed ($ratio:int) :void {
			if(globalVars.isPlaying) {
				sc.stop();
			}
			//music = new Sound(request);
			sc = new SoundChannel();
			sc = music.play($ratio*soundLength);
			globalVars.isPlaying = true;
			trace("scrubSong");
		}
		public function broadcastCountdown () :String {
			var str:String = "0:00";
			if (globalVars.isPlaying) {
				var seconds:int = Math.ceil((soundLength - sc.position)/1000);
				var minutes:int = Math.floor(seconds /60);
				seconds -=  minutes*60;
				if(seconds > 10) {
					str = String(minutes+":"+seconds);
				} else {
					str = String(minutes+":0"+seconds);
				}
			}
			return (str);
		}
		
	}
}